Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

331
Vistas
How do I loop through this number array to find if a number is <25 or >= 25?

Apologies in advance if this is too simple. It should say "Small number" nine times and "Big Number!" two times. Instead it hangs on the console.log. Please tell me what I'm doing wrong.

let numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
    for (let i = 0; i < numArray.length; i++);
      let message = (numArray[i] > 25)? "Big Number!":
                    (numArray[i] <= 25)? "Small number"
                   console.log(message)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your code refers only to numArray as a whole, never to the i'th entry in numArray. You probably want to compare to numArray[i] or similar, rather than just numArray.

(also, may want to tag what language you're programming in, in case there are language-specific issues, but I'm pretty sure that issue will be a bug in any language)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Assuming that this is javascript (or typescript perhaps) I see a few problems here. First of the semicolon at the end of the for statement is ending the loop altogether without being executed as a code block you should remove it if you want to iterate through the array numArray

for (let i = 0; i < numArray.length; i++)

Second, after you remove the semicolon at the end of that line if you intended to execute both the ternary operation (it didn't work for me on javascript if this is another language, it would be great if you can update your question and clarify that) and output to the console, you should enclose both lines in brackets, like so:

{
    let message = (numArray[i] > 25) ? "Big Number!" : "Small number";
    console.log(message)
}

And of course, you are comparing the whole array not just an element, so you need to get an item using the index

   numArray[i] > 25 ...

With those corrections, I can get the output that you are expecting (Small number nine times and Big number two times)


for the sake of clarity I've added (What i see) as the fixed code (in javascript/typescript) right below:

let numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
for (let i = 0; i < numArray.length; i++) {
  let message = (numArray[i] > 25) ? "Big Number!" : "Small number";
  console.log(message)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You forgot to wrap your logic inside the for loop you actually forgot the { } and no need to specify the second condition just put else.

const numArray = [4, 2, 5, 10, 5, 3, 7, 22, 3, 70, 37];
for (let i = 0; i < numArray.length; i++) {
  let message = numArray[i] > 25 ? "Big Number!" : "Small number"
  console.log(message)
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda